package com.zhou.imaotai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zhou.imaotai.entity.IUser;
import com.zhou.imaotai.entity.R;
import com.zhou.imaotai.mapper.IUserMapper;
import com.zhou.imaotai.service.IUserService;
import com.zhou.imaotai.utils.PageResult;
import com.zhou.imaotai.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author lcz
 * @desc 用户服务impl
 * @mail lcz_0130@163.com
 * @date 2024/03/21
 */

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper iUserMapper;

    /**
     * 查询I茅台用户列表
     *
     * @param iUser i用户
     * @return {@link PageResult}<{@link IUser}>
     */
    @Override
    public PageResult<IUser> page(IUser iUser) {
        return iUserMapper.selectPage(iUser);
    }

    /**
     * 插入用户
     *
     * @param mobile     电话
     * @param deviceId   设备id
     * @param jsonObject json对象
     * @return int
     */
    @Override
    public int insertIUser(Long mobile, String deviceId, JSONObject jsonObject) {
        IUser user = iUserMapper.selectById(mobile);

        if (user != null) {
            //存在则更新
            IUser iUser = new IUser(mobile, jsonObject);
            BeanUtil.copyProperties(iUser, user, "shopType", "minute");
            return iUserMapper.updateById(user);
        } else {
            if (StringUtils.isEmpty(deviceId)) {
                deviceId = UUID.randomUUID().toString().toLowerCase();
            }
            IUser iUser = new IUser(mobile, deviceId, jsonObject);
            return iUserMapper.insert(iUser);
        }


    }

    /**
     * 查询预约用户列表
     *
     * @return {@link List}<{@link IUser}>
     */
    @Override
    public List<IUser> selectReservationUser() {
        return iUserMapper.selectReservationUser();

    }

    /**
     * 通过预约执行分钟查询预约用户列表
     *
     * @param minute 分钟
     * @return {@link List}<{@link IUser}>
     */
    @Override
    public List<IUser> selectReservationUserByMinute(int minute) {
        return iUserMapper.selectReservationUserByMinute(minute);
    }

    /**
     * 添加i茅台用户
     *
     * @param iUser i用户
     * @return {@link R}
     */
    @Override
    public R insertIUser(IUser iUser) {

        IUser user = iUserMapper.selectById(iUser.getMobile());
        if (user != null) {
            return R.error("用户已存在");
        }

        if (StringUtils.isEmpty(iUser.getDeviceId())) {
            iUser.setDeviceId(UUID.randomUUID().toString().toLowerCase());
        }
        int insert = iUserMapper.insert(iUser);
        return R.ok("添加成功");
    }

    /**
     * 修改I茅台用户
     *
     * @param iUser i用户
     * @return int
     */
    @Override
    public int updateIUser(IUser iUser) {
//        if (SecurityUtils.getUserId() != 1 && !iUser.getCreateUser().equals(SecurityUtils.getUserId())) {
//            throw new ServiceException("只能修改自己创建的用户");
//        }
        return iUserMapper.updateById(iUser);
    }

    /**
     * 批量修改用户随机预约的时间
     */
    @Override
    @Async
    public void updateUserMinuteBatch() {
        Long userCount = iUserMapper.selectCount();
        if (userCount > 60) {
            iUserMapper.updateUserMinuteEven();
        } else {
            iUserMapper.updateUserMinuteBatch();
        }
    }

}
